package com.home.webapp.base.service.impl;

import com.home.common.constants.redis.RedisDateBase;
import com.home.common.redis.RedisUtil;
import com.home.webapp.base.mapper.BaseDictDataMapper;
import com.home.webapp.base.mapper.BaseDictTypeMapper;
import com.home.webapp.base.service.InitCacheService;
import com.home.webapp.common.constants.dict.DictDataStatusConstant;
import com.home.webapp.common.entity.DictDataOut;
import com.home.webapp.common.entity.DictTypeOut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Description:
 * Created by 王大宸 on 2020-12-27 21:43
 * Created with IntelliJ IDEA.
 */
@Service
public class InitCacheServiceImpl implements InitCacheService {
    private static final Logger logger = LoggerFactory.getLogger(InitCacheServiceImpl.class);
    @Autowired
    private BaseDictTypeMapper dictTypeMapper;
    @Autowired
    private BaseDictDataMapper dictDataMapper;

    /***
     * 功能说明：初始化数据字典
     *
     * @author 王大宸
     * @date 2020/10/30 14:34
     * @param
     * @return void
     */
    @Override
    public void initDict() {
        logger.info("正在初始化数据字典...");
        // 初始化数据字典类型
        List<DictTypeOut> dictTypes = dictTypeMapper.selectDictType();
        for (DictTypeOut dictType : dictTypes) {
            RedisUtil.set(RedisDateBase.DICT_DATA_REDIS_KEY + dictType.getDictType(), dictType, RedisDateBase.DEFAULT_DATE_BASE);
            // 初始化数据字典
            List<DictDataOut> dictDataList = dictDataMapper.getType(dictType.getDictType(), DictDataStatusConstant.IS_OK);
            RedisUtil.set(RedisDateBase.DICT_DATA_REDIS_KEY + dictType.getDictType() + RedisDateBase.CHILDREN_SUFFIX, dictDataList, RedisDateBase.DEFAULT_DATE_BASE);
            for (DictDataOut data : dictDataList) {
                RedisUtil.set(RedisDateBase.DICT_DATA_REDIS_KEY + data.getDictType() + "_" + data.getDictValue(), data, RedisDateBase.DEFAULT_DATE_BASE);
            }
        }
        logger.info("数据字典初始化完毕...");
    }

}
